package com.wbao.dianniu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AnswerPraiseManager;
import com.wbao.dianniu.manager.KnowAnswerGoodManger;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.AnswerListType;
import com.wbao.dianniu.utils.CommentLongClickHelper;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.ReportType;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 二级回复列表适配器
 * <p>
 * Created by yejianjun on 2018/6/5.
 */

public class TwoCommentAdapter extends BaseAdapter {
    private Context mContext;
    private List<QuestAnswerListData> mList = new ArrayList<>();
    private LayoutInflater mInflater;
    private int type;

    public TwoCommentAdapter(Context context, int type) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        this.type = type;
    }

    public void addData(List<QuestAnswerListData> list) {
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public List<QuestAnswerListData> getList() {
        return mList;
    }

    public void clear() {
        if (null != mList) {
            mList.clear();
        }
    }

    public void addOneData(QuestAnswerListData data) {
        if (null != mList) {
            mList.add(0, data);
        }
    }

    public void deleteOneData(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void deleteById(int id) {
        if (null == mList)
            return;
        for (int i = 0; i < mList.size(); i++) {
            if (id == mList.get(i).id) {
                mList.remove(i);
                break;
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.two_comment_list_item, null);
            holder = new ViewHolder();
            holder.headIV = (ImageView) convertView.findViewById(R.id.user_head);
            holder.nameTV = (TextView) convertView.findViewById(R.id.user_name);
            holder.sexIV = (ImageView) convertView.findViewById(R.id.user_sex);
            holder.partnerIV = (ImageView) convertView.findViewById(R.id.user_partner);
            holder.labelTV = (TextView) convertView.findViewById(R.id.user_label);
            holder.addV = (ImageView) convertView.findViewById(R.id.add_v_iv);
            holder.labelRelative = (RelativeLayout) convertView.findViewById(R.id.user_small_relative);
            holder.praiseTV = (TextView) convertView.findViewById(R.id.quest_answer_praise_tv);
            holder.contentTV = (SpanTextView) convertView.findViewById(R.id.comment_list_content);
            holder.sandianIV = (ImageView) convertView.findViewById(R.id.quest_answer_comment_sandian);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        QuestAnswerListData data = mList.get(position);

        if (null != data) {
            PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.userInfo.headPic);
            holder.nameTV.setText(data.userInfo.realName == null ? "" : data.userInfo.realName);
            if (null != data.anonymous && 1 == data.anonymous) {
                holder.labelRelative.setVisibility(View.GONE);
                holder.sexIV.setVisibility(View.GONE);
                holder.partnerIV.setVisibility(View.GONE);
            } else {
                holder.labelTV.setText(data.userInfo.label == null ? "" : data.userInfo.label);
                Utils.showAuth(data.userInfo, holder.addV);
                if (null == data.userInfo.sex) {
                    Utils.showSex(mContext, null, holder.sexIV);
                } else {
                    Utils.showSex(mContext, data.userInfo.sex, holder.sexIV);
                }

                Utils.showPartner(mContext, data.userInfo.partner, holder.partnerIV);
            }

            Utils.praiseChanged(mContext, data.isGood, data.goodCount, holder.praiseTV);
            holder.contentTV.setEmojiText(data.content, data.noticeJson);

        }

        //点击事件
        View.OnClickListener headListener = new clickListener(data);
        holder.headIV.setOnClickListener(headListener);
        holder.contentTV.setOnClickListener(headListener);
        holder.sandianIV.setOnClickListener(headListener);
        holder.praiseTV.setOnClickListener(new praiseListener(position, data));

        return convertView;
    }

    /**
     * 点击功能
     */
    class clickListener implements View.OnClickListener {
        private QuestAnswerListData data;

        public clickListener(QuestAnswerListData data) {
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_head:
                case R.id.user_head_layout:
                    if (type == AnswerListType.TYPE_AM && 1 == data.anonymous) {
                    } else {
                        ActivityUtils.intoHeadInfoActivity(data.userInfo.accountId, mContext);
                    }
                    break;
                case R.id.quest_answer_comment_sandian:
                    // 弹出复制框等....
                    CommentLongClickHelper commentLongClickHelper = CommentLongClickHelper.getInstance();
                    if (type == AnswerListType.TYPE_KNOW) {
                        commentLongClickHelper.commentLongClick(mContext, data, ReportType.TYPE_KNOW_REPLY, data.userInfo.realName, 2, AnswerListType.TYPE_KNOW);
                    } else {
                        commentLongClickHelper.commentLongClick(mContext, data, ReportType.TYPE_REPLY, data.userInfo.realName, 2, AnswerListType.TYPE_QUEST);
                    }
                    break;
                default:
                    break;
            }

        }

    }

    class praiseListener implements View.OnClickListener {
        private int position;
        private QuestAnswerListData data;

        public praiseListener(int position, QuestAnswerListData data) {
            this.position = position;
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.quest_answer_praise_tv) {
                if (1 == data.isGood) {
                    data.goodCount--;
                    data.isGood = 0;
                } else {
                    data.goodCount++;
                    data.isGood = 1;
                }
                notifyUI(position, data);

                //通知后台更新数据
                if (type == AnswerListType.TYPE_KNOW) {
                    KnowAnswerGoodManger goodManger = new KnowAnswerGoodManger(mContext);
                    goodManger.knowAnswerGood(GlobalContext.getAccountId(), data.id);
                } else if (type == AnswerListType.TYPE_QUEST || type == AnswerListType.TYPE_AM) {
                    AnswerPraiseManager manager = new AnswerPraiseManager(mContext);
                    manager.answerPraise(GlobalContext.getAccountId(), data.id);
                }

            }

        }

    }

    private void notifyUI(int position, QuestAnswerListData data) {
        if (null != data && null != mList) {
            mList.set(position, data);
        }
        notifyDataSetChanged();
    }


    private class ViewHolder extends UserInfoHolder {
        /**
         * 点赞数
         */
        public TextView praiseTV;
        /**
         * 回复内容
         */
        private SpanTextView contentTV;

        private ImageView sandianIV;
    }

}
